package com.example.locationreport.locationreport.utils;

import android.os.Environment;

import java.io.File;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * author : King
 * date   : 2023/11/919:10
 * desc   :
 */
public class LogLocationUtils {

    private static String logPath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "LocationReport/";

    private static int LOG_FILE_SAVE_DAYS = 3;

    public static String writeLog(String fileName , String fileContent){

        LogUtils.e(fileName + Thread.currentThread().getName() , fileContent);

        String filePath = logPath + fileName + "-" + getDate() + ".txt";

        try {
            File dictionaryFile = new File(logPath);//这是目录路径
            if (!dictionaryFile.exists()) {
                dictionaryFile.mkdirs();
            }

            File textFile = new File(filePath);//这是文件路径

            if(!textFile.exists()){
                textFile.createNewFile();
            }

            RandomAccessFile raf = new RandomAccessFile(filePath, "rwd");
            raf.seek(textFile.length());

            raf.write((getTime() + " : " + Thread.currentThread().getName() + "..." + fileContent + "\n").getBytes());
            raf.close();
            return filePath;
        } catch (Exception e) {
            return "";
        }
    }


    public static void deleteLog(){

        File dictionaryFile = new File(logPath);//这是目录路径

        if (!dictionaryFile.exists())
            return;

        if(dictionaryFile.listFiles() == null || dictionaryFile.listFiles().length == 0)
            return;

        for(File file : dictionaryFile.listFiles()) {
            if (file != null) {
                long diff = Calendar.getInstance().getTimeInMillis() - getCalendarFromDate(file.getName().split("-")[1]).getTimeInMillis();
                if ((diff / (24 * 60 * 60 * 1000)) > LOG_FILE_SAVE_DAYS) {
                    file.delete();
                }
            }
        }
    }

    /**
     * 获取当天
     * @return
     */
    public static String getDate(){
        Calendar calendar = Calendar.getInstance();

        return calendar.get(Calendar.YEAR) + "_" + (calendar.get(Calendar.MONTH) + 1) + "_" + calendar.get(Calendar.DAY_OF_MONTH);
    }


    /**
     * 获取时间字符串
     * @return
     */
    public static String getTime(){
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        return simpleDateFormat.format(date);
    }

    private static Calendar getCalendarFromDate(final String dateTime) {

        String date = dateTime.split("\\.")[0];

        int year = 0;
        int month = 0;
        int day = 0;

        try {
            String[] array = date.split("_");

            int[] arrayInt = new int[array.length];

            for (int i = 0; i < array.length; i++) {
                arrayInt[i] = Integer.parseInt(array[i]);
                if(i == 0) {
                    year = arrayInt[0];
                } else if(i == 1){
                    month = arrayInt[1];
                } else if(i == 2){
                    day = arrayInt[2];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        if(year > 0 && month >= 0 && day >= 0) {
            cal.set(year, month - 1, day , 0 , 0 , 0);
        }
        return cal;
    }
}
